
//package名和导入包路径可能要修改
package OSMS.dao;
import java.util.ArrayList;
import java.util.List;

import OSMS.entity.Customers;
import OSMS.entity.Types;
import OSMS.tools.JDBC_Util;

public class CustomerDao {
	// 根据买家id号获取Customer对象
	public Customers  getCusById(Integer cid) throws Exception{
		String sql = "select * from customer where cid = ?";
		Customers cus = null;
		cus = JDBC_Util.queryBean(sql, Customers.class,cid);
		return cus;
	}
	
	//修改个人信息时使用：修改已存在的买家的数据
	//	传入一整个Customers对象，所以使用前需要先把数据封装进Customers对象里
	public boolean updateCus(Customers cus) throws Exception{
		String sql = "update customer set cname = ?,tel = ?,address = ?,sex = ?,cpwd = ? where cid = ?";
		boolean flag = false;
		flag = JDBC_Util.updateDb(sql, cus.getCname(),cus.getTel(),cus.getAddress(),cus.getSex(),cus.getCpwd(),cus.getCid());
		return flag;
	}
	
	//注册时使用：插入买家的数据
	//	传入一整个Customers对象，所以使用前需要先把数据封装进Customers对象里
	public boolean insertCus(Customers cus)throws Exception{
		String sql = "insert into customer values(null,?,?,?,?,?)";
		boolean flag = false;
		flag = JDBC_Util.updateDb(sql,cus.getCname(),cus.getTel(),cus.getAddress(),cus.getSex(),cus.getCpwd());
		return flag;
	}
	
	
	//登录时使用，匹配数据库中的手机号和密码
	public Customers getCusWhenLogin(String tel,String cpwd) throws Exception{
		String sql = "select * from customer where tel = ? and cpwd = ?";
		Customers cus = null;
		cus = JDBC_Util.queryBean(sql, Customers.class,tel,cpwd);
		return cus;
	}
	
	//注册时使用，匹配数据库中的手机号，检测该手机号是否已注册

	public List<Customers> getCustomersByTel(String tel) throws Exception {
		String sql = "select * from customer where TEL = ?" ;
		List<Customers> CustomersList = JDBC_Util.queryBeanList(sql, Customers.class, tel);
		return CustomersList ;
	}

	/*7.11 刘艺杰新增
	 * 根据手机号（手机号唯一）查找用户
	 * 
	 */
	public Customers  getCusByTel(String tel) throws Exception{
		String sql = "select * from customer where tel = ?";
		Customers cus = null;
		cus = JDBC_Util.queryBean(sql, Customers.class,tel);
		return cus;
	}
	
	
	/* 7.11 刘艺杰 新增
	 * 查询type表中商品类型的种类
	 * 
     */
	public ArrayList<Types> typeList()throws Exception{
		String sql = "select * from type";
		ArrayList<Types> typesList = (ArrayList<Types>)JDBC_Util.queryBeanList(sql, Types.class);
		return typesList;
	}
	/*7.11 刘艺杰新增
	 * 注册用户时，在“猜你喜欢表中”插入所有商品类型的数据
	 * 
	 */
	public boolean initClike(int cid) throws Exception{
		ArrayList<Types> typesList = this.typeList();
		int num = typesList.size();
		String sql = "insert into clike values(?,?,0)";
		boolean flag = true;
		int cnt = 0;
		for(int i = 0;i<num&&flag;i++)
		{
			flag = JDBC_Util.updateDb(sql, cid,typesList.get(i).getTid());
			cnt++;
		}
		if(cnt==num)
			return true;
		return false;
	}
	
	
	/*7.11 刘艺杰新增
	 * 根据用户cid 查询clike表中该用户浏览最多的商品类型
	 * 
	 */
	public Types mostLiketype(int cid) throws Exception{
		Types type = new Types();
		String sql = "select clike.TID,type.TNAME from clike,type where cid = ? and type.TID=clike.TID order by VISIT_NUM limit 1";
		type = JDBC_Util.queryBean(sql, Types.class, cid);
		return type;
	}
	
	
	/*7.13 刘艺杰新增
	 * 更改用户密码
	 * 
	 */
	public boolean upDateCusPwd(String cpwd,int cid) throws Exception{
		String sql = "update customer set cpwd = ? where cid = ?";
		boolean flag = false;
		flag = JDBC_Util.updateDb(sql, cpwd,cid);
		return flag;
	}
	
	/*7.13 刘艺杰新增
	 * 更改用户密码
	 * 
	 */
	public boolean upDateCusMsg(String username,String sex,String address,int cid) throws Exception{
		String sql = "update customer set cname = ?,sex = ?,address = ? where cid = ?";
		boolean flag = false;
		flag = JDBC_Util.updateDb(sql, username,sex,address,cid);
		
		return flag;
	}
}
